package im.yisme.view.ui.util;

import java.util.ArrayList;

import android.graphics.PointF;

public class PointChecker {

	public static boolean isInside(ArrayList<PointF> points, PointF a){
		
		double realArea = getRealArea(points);
		double toCheckArea = getToCheckArea(points, a);
		
		double rate = Math.abs((realArea - toCheckArea))/realArea;		
		return rate < 0.05;
	}
	
	private static double getRealArea(ArrayList<PointF> points){
		
		double area = 0;
		PointF root = points.get(0);
		
		for (int i=1; i<points.size()-1; i++){
			area += TriangleArea.getArea(root, points.get(i), points.get(i + 1));
		}
		
		return area;
	}
	
	private static double getToCheckArea(ArrayList<PointF> points, PointF root){
		double area = 0;
		
		for (int i=0; i<points.size(); i++){
			int j = i == points.size() - 1 ? 0 : i+1;
			area += TriangleArea.getArea(root, points.get(i), points.get(j));
		}
		
		return area;
	}
}
